<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="基地名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入基地名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请入驻用户姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入申请入驻用户姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请入驻用户身份证" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入申请入驻用户身份证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['GoApply:GoApply:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="GoApplyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="基地名" align="center" prop="name" />
      <el-table-column label="基地简介" align="center" prop="info" />
      <el-table-column
        label="申请入驻用户姓名"
        align="center"
        prop="nickname"
      />
      <el-table-column label="申请入驻用户电话" align="center" prop="phone" />
      <el-table-column label="申请入驻用户身份证" align="center" prop="email" />
      <el-table-column
        label="身份证正面"
        align="center"
        prop="idcardimg"
        width="100"
      >
        <template #default="scope">
          <image-preview :src="scope.row.idcardimg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="身份证背面"
        align="center"
        prop="idcardimg2"
        width="100"
      >
        <template #default="scope">
          <image-preview :src="scope.row.idcardimg2" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="基地图片" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="设施完善情况" align="center" prop="sheshi">
        <template #default="scope">
          <dict-tag :options="sheshi" :value="scope.row.sheshi" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['GoApply:GoApply:edit']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请表管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="applyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item
          label="是否通过(否/是)"
          align="center"
          prop="isOk"
          width="350px"
        >
          <el-switch v-model="isOk" />
        </el-form-item>
        <br />
        <br />
        <el-form-item
          label="审核未通过原因"
          align="center"
          prop="isOk"
          width="350px"
          v-if="!isOk"
        >
          <el-input v-model="inputCause" width="150"> </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="GoApply">
 
const isOk = ref(false);
const inputCause = ref("");
import { onMounted } from "vue";
import { useStore } from "vuex";
const store = useStore();
const sendMsg = (data) => {
  let msgObj = {
    to: form.value.username,
    content: isOk.value
      ? "您的入驻申请已被授权,系统为您分配的用户名为"+"您申请入驻填写的电话："+data.username+"  密码为"+data.password+" 系统将在24小时内为您分配基地负责人权限，将通过邮件形式通知"
      : "您的入驻申请审核未通过，原因为：" + inputCause.value,
    notSelf: false,
  };
  store.state.stomp.send("/ws/success", {}, JSON.stringify(msgObj));
};

const sendRegisterErrorMsg = (data) => {
  let msgObj = {
    to: form.value.username,
    content: data,
    notSelf: false,
  };
  store.state.stomp.send("/ws/success", {}, JSON.stringify(msgObj));
};
 

import {
  listGoApply,
  getGoApply,
  delGoApply,
  addGoApply,
  updateGoApply,
} from "@/api/GoApply/GoApply";

const { proxy } = getCurrentInstance();
const { sheshi } = proxy.useDict("sheshi");

const GoApplyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    nickname: null,
    email: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询入驻申请列表 */
function getList() {
  loading.value = true;
  listGoApply(queryParams.value).then((response) => {
    GoApplyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    info: null,
    nickname: null,
    phone: null,
    email: null,
    idcardimg: null,
    idcardimg2: null,
    img: null,
    sheshi: null,
    status: null,
    cause: null,
    see: null,
  };
  proxy.resetForm("GoApplyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入驻申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getGoApply(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    isOk.value=false;
    inputCause.value="";
    title.value = "是否同意授权"+form.value.username+"该用户入驻申请";
  });
}

/** 提交按钮 */
function submitForm() {
  form.value.status=isOk.value
  if (form.value.id != null) {
    updateGoApply(form.value).then((response) => {
      console.log(response);
      let data=''
      if(response.msg=='success'){
        if(isOk.value){
          proxy.$modal.msgSuccess('审核成功');
          data=response.data
        }else{
          proxy.$modal.msgError('审核失败,原因为'+inputCause.value);
        }
        sendMsg(data);
      }else{
        proxy.$modal.msgError(response.data);
        sendRegisterErrorMsg(response.data);
      }
      open.value = false;
      getList();
    });
  } else {
    addGoApply(form.value).then((response) => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除入驻申请编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delGoApply(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "GoApply/GoApply/export",
    {
      ...queryParams.value,
    },
    `GoApply_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
